/** Copyright (c) 2012, peter.ducai@gmail.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the Peter Ducai nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL PETER "daemonna" DUCAI BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.daemonna.fluentlee_core.managers;

import com.daemonna.fluentlee_core.job.job;
import com.daemonna.fluentlee_core.job.schedule.status;
import com.daemonna.fluentlee_core.pools.schedule_pool;
import com.daemonna.fluentlee_core.pools.server_settings_pool;
import com.daemonna.fluentlee_core.reporting.HTMLprocessor;

/*
 * @author peter 'daemonna' ducai @author peter.ducai@gmail.com @author
 * http://code.google.com/p/fluentlee/
 */
public class report_manager {


    public report_manager() {
            process_html_reports();
    }

    private void process_html_reports() {
        //System.out.println("processing report");

        for (int z = 0; z < schedule_pool.getInstance().scheds.length; z++) {

            if (schedule_pool.getInstance().scheds[z] != null) {
                if (schedule_pool.getInstance().scheds[z].state == status.COMPLETED) {

                    System.out.println("REPORTING:" + schedule_pool.getInstance().scheds[z].name);
                    //mark schedule for reporting
                    schedule_pool.getInstance().scheds[z].state = status.REPORTED;

                    HTMLprocessor html = new HTMLprocessor("reports/" + schedule_pool.getInstance().scheds[z].name + "_schedule_report.html");
                    html.write_HEAD(schedule_pool.getInstance().scheds[z].name);

                    //summary table
                    html.start_table(600);

                    String suitecount = Integer.toString(schedule_pool.getInstance().scheds[z].suite_count);
                    String jobcount = Integer.toString(schedule_pool.getInstance().scheds[z].job_count);
                    String[] zzz = {"file: " + schedule_pool.getInstance().scheds[z].name, "suites: " + suitecount, "jobs: " + jobcount};
                    html.write_table_header(zzz);
                    html.end_table();
                    html.write_br();

                    html.start_table(800);
                    String[] ff = {"suite/job name", "comment", "start", "end", "elapsed", "result"};
                    html.write_table_header(ff);

                    for (int x = 0; x < schedule_pool.getInstance().scheds[z].suites.length; x++) {
                        if (schedule_pool.getInstance().scheds[z].suites[x] != null) {

                            String[] dd = {schedule_pool.getInstance().scheds[z].suites[x].name};
                            html.write_table_header(dd);

                            for (int xx = 0; xx < schedule_pool.getInstance().scheds[z].suites[x].jobs.length; xx++) {
                                if (schedule_pool.getInstance().scheds[z].suites[x].jobs[xx] != null) {

                                    String comm = schedule_pool.getInstance().scheds[z].suites[x].jobs[xx].comment;
                                    String start = String.valueOf(schedule_pool.getInstance().scheds[z].suites[x].jobs[xx].start_time);
                                    String end = String.valueOf(schedule_pool.getInstance().scheds[z].suites[x].jobs[xx].end_time);
                                    String elaps = String.valueOf(schedule_pool.getInstance().scheds[z].suites[x].jobs[xx].end_time-schedule_pool.getInstance().scheds[z].suites[x].jobs[xx].start_time);
                                    
                                    String res;                                    
                                    if(schedule_pool.getInstance().scheds[z].suites[x].jobs[xx].result == job.jobresult.PASS){
                                        res = " class=\"pass\">PASS";
                                    } else {
                                        res = " class=\"fail\"><i>FAIL</i>";
                                    }

                                    String[] fff = {schedule_pool.getInstance().scheds[z].suites[x].jobs[xx].name, comm, start, end, elaps, res};
                                    html.write_table_row(fff);
                                }
                            }
                        }
                    }

                    html.end_table();
                    html.write_br();
                    html.write_FOOT();
                    
                }

                if (schedule_pool.getInstance().scheds[z].state == status.REPORTED) {
                    //System.out.println("already reported... "+schedule_pool.getInstance().scheds[z].name);
                }
            }
        }

        refresh_main_HTML();
    }

    private void refresh_main_HTML() {

        String[] maindata = new String[10];

        String eng = server_settings_pool.getInstance().engine_name;
        String ver = server_settings_pool.getInstance().docserv_version;
        String[] mainvals = {"Web Server", "engine:" + eng, "version:" + ver, "running OK"};

        HTMLprocessor mhtml = new HTMLprocessor("www/main.html");
        mhtml.write_HEAD("Fluentlee Web Server");

        mhtml.start_table(700);
        mhtml.write_table_header(mainvals);
        mhtml.end_table();
        mhtml.write_br();
        
        
        String[] ff = {"file name", "comment", "status", "report", "start", "end", "elapsed", "suites", "jobs", "result"};
        mhtml.start_table(900);
        mhtml.write_table_header(ff);

        for (int z = 0; z < schedule_pool.getInstance().scheds.length; z++) {

            if (schedule_pool.getInstance().scheds[z] != null) {
                if (schedule_pool.getInstance().scheds[z].state == status.COMPLETED) {
                    
                    maindata[0] = schedule_pool.getInstance().scheds[z].name;
                    maindata[1] = "N/A";
                    maindata[2] = "COMPLETED but not reported";                    
                    maindata[3] = "report: <a href=\"reports/" + schedule_pool.getInstance().scheds[z].name + "_schedule_report.html\">" + schedule_pool.getInstance().scheds[z].name + "_schedule_report.html</a>";
                    maindata[4] = String.valueOf(schedule_pool.getInstance().scheds[z].start_time);
                    maindata[5] = String.valueOf(schedule_pool.getInstance().scheds[z].end_time);
                    maindata[6] = String.valueOf(schedule_pool.getInstance().scheds[z].end_time-schedule_pool.getInstance().scheds[z].start_time);
                    maindata[7] = String.valueOf(schedule_pool.getInstance().scheds[z].suite_count);
                    maindata[8] = String.valueOf(schedule_pool.getInstance().scheds[z].job_count);
                    maindata[9] = "PASS";
                    
                    mhtml.write_table_row(maindata);
                }

                if (schedule_pool.getInstance().scheds[z].state == status.ACTIVE) {
                    maindata[0] = schedule_pool.getInstance().scheds[z].name;
                    maindata[1] = "N/A";
                    maindata[2] = "ACTIVE";                    
                    maindata[3] = "report still NOT generated";
                    maindata[4] = String.valueOf(schedule_pool.getInstance().scheds[z].start_time);
                    maindata[5] = String.valueOf(schedule_pool.getInstance().scheds[z].end_time);
                    maindata[6] = String.valueOf(schedule_pool.getInstance().scheds[z].end_time-schedule_pool.getInstance().scheds[z].start_time);
                    maindata[7] = String.valueOf(schedule_pool.getInstance().scheds[z].suite_count);
                    maindata[8] = String.valueOf(schedule_pool.getInstance().scheds[z].job_count);
                    maindata[9] = "PASS";
                    
                    mhtml.write_table_row(maindata);
                }



                if (schedule_pool.getInstance().scheds[z].state == status.REPORTED) {
                    maindata[0] = schedule_pool.getInstance().scheds[z].name;
                    maindata[1] = "N/A";
                    maindata[2] = "allready REPORTED";                    
                    maindata[3] = "report: <a href=\"reports/" + schedule_pool.getInstance().scheds[z].name + "_schedule_report.html\">" + schedule_pool.getInstance().scheds[z].name + "_schedule_report.html</a>";
                    maindata[4] = String.valueOf(schedule_pool.getInstance().scheds[z].start_time);
                    maindata[5] = String.valueOf(schedule_pool.getInstance().scheds[z].end_time);
                    maindata[6] = String.valueOf(schedule_pool.getInstance().scheds[z].end_time-schedule_pool.getInstance().scheds[z].start_time);
                    maindata[7] = String.valueOf(schedule_pool.getInstance().scheds[z].suite_count);
                    maindata[8] = String.valueOf(schedule_pool.getInstance().scheds[z].job_count);
                    maindata[9] = "PASS";
                    
                    mhtml.write_table_row(maindata);

                }
            }
            
            
        }
        mhtml.end_table();
        mhtml.write_FOOT();

    }
    
    /*
     * create JSON version of reports (for example for web service)
     */
    public void process_json_reports() {
        
    }
    
    /*
     * create pure XML version of reports 
     */
    public void process_xml_reports() {
        
    }
}
